<?php
@session_start();
require_once('../config/config.php');
require_once('../utils/CommonUtils.php');
require_once('../utils/DBUtils.php');
include ("../utils/Constants.php");
require_once('../utils/BaseDAO.php');
require_once('../utils/Pager.php');
require("../utils/Session.php");

$commonUtils = new CommonUtils();
$onlineUser = Session::get('fullname');
$isuser = Session::get('isuser');
if($onlineUser == null){
	$commonUtils->redirect("../login.php"); 
}

$baseDAO = new BaseDAO();
$pager = new Pager();

$query = "1";
if(isset($isuser) && $isuser == 0){
	$userid = Session::get('userid');
	if(isset($userid)){
		$query = "createdby=".$userid;
	}	
}
$action = $commonUtils->getParameter("action");
$page = $commonUtils->getParameter("p", 1);
$sortExpression = $commonUtils->getParameter("s", "ct.createddate");
$sortDirection = $commonUtils->getParameter("d", "desc");


if(isset($action) && $action == 'delete'){
	$checkList = $commonUtils->getParameter("checkList");
	if (isset($checkList) && is_array($checkList)) {
		foreach($checkList as $id) {
			$baseDAO->del("comment", $id, "contentid");
			$baseDAO->del("content", $id);
		}	
	}
}else if(isset($action) && $action == 'search'){
	$title = $commonUtils->getParameter("title");
	if (isset($title) && $title != '') {
		$query .= " AND title LIKE '%" . $title . "%'";
	}
}

if (isset($sortExpression) && $sortExpression != '') {
	$orderby = " ORDER BY $sortExpression $sortDirection";
}
$items = $baseDAO->getContents(array("id", "title", "createddate", "updateddate", "createdby", "updatedby", "status", "views", "likes"), $query, $orderby, ($page - 1) * $pageSize, $pageSize);
$totalItems = $baseDAO->getTotalItems("content", $query);

$categories = $baseDAO->getItems("category", array("id", "name"));
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=100" >
		<title>NguyenDau - Quản lý nội dung</title>
		<link rel="stylesheet" type="text/css" href="../css/diary.css"/>
		<link rel="stylesheet" type="text/css" href="../css/jquery.alert.css"/>
		<script src="../js/jquery-1.7.2.min.js"></script>
		<script src="../js/jquery.alerts.js"></script>
		<script src="../js/global.js"></script>
	</head>
	<body>
		<?php include '../include/header.php';?>
		<center>
			<div class="content">
				<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post" name="formUserList" id="formUserList" >
					<div class="tableHeader">
						Danh sách nội dung
					</div>
					<div class="searchfilter">
						<table>
							<tr>
								<td width="120px">Tiêu đề</td>
								<td><input type="text" name="title" value="<?php if(isset($title) && $title != ""){ echo $title;} ?>"/></td>
								<td width="120px"></td>
								<td></td>
							</tr>
							<tr>
								<td></td>
								<td colspan="3"><input type="button" id="btSearch" value="Tìm kiếm"/></td>
							</tr>
						</table>
					</div>
					<table cellspacing="0" cellpadding="0" class="tableList">
						<tr class="trHeader">
							<td width="20px"></td>
							<td><a href="<?php $pager->drawSortBy("title") ?>">Tiêu đề</a></td>
							<td width="8%"><a href="<?php $pager->drawSortBy("views") ?>">Lượt xem</a></td>
							<td width="5%"><a href="<?php $pager->drawSortBy("status") ?>">Hiển thị</a></td>
							<td width="8%"><a href="<?php $pager->drawSortBy("createddate") ?>">Ngày tạo</a></td>
							<?php if(isset($isuser) && $isuser != 0){?>
								<td width="8%"><a href="<?php $pager->drawSortBy("createdby") ?>">Người tạo</a></td>
							<?php }?>
							<td width="8%"><a href="<?php $pager->drawSortBy("updateddate") ?>">Ngày cập nhật</a></td>
							<?php if(isset($isuser) && $isuser != 0){?>
								<td width="9%"><a href="<?php $pager->drawSortBy("updatedby") ?>">Người cập nhật</a></td>
							<?php }?>
							<td width="80px">Thao tác</td>
						</tr>
						<?php $rowcss = 'even';?>
						<?php foreach($items as $item){ ?>
						<?php 
							if($rowcss == 'even'){
								$rowcss = 'odd';
							}else{
								$rowcss = 'even';
							}
						?>
						<tr class="<?php echo $rowcss;?>">
							<td><input type="checkbox" name="checkList[]" value="<?php echo $item['id'];?>"/></td>
							<td><?php echo $item['title']?></td>
							<td><?php echo $item['views']?></td>
							<td><?php if($item['status'] == 1){echo '<span style="color:green">Có</span>';}else{echo '<span style="color:red">Không</span>';}?></td>
							<td><?php echo $commonUtils->formatDate($item['createddate'], "d/m/Y");?></td>
							<?php if(isset($isuser) && $isuser != 0){?>
								<td><?php echo $item['createdby']?></td>
							<?php }?>
							<td><?php echo $commonUtils->formatDate($item['updateddate'], "d/m/Y");?></td>
							<?php if(isset($isuser) && $isuser != 0){?>
								<td><?php echo $item['updatedby']?></td>
							<?php }?>
							<td><a href="./content-edit.php?id=<?php echo $item['id'];?>">Chỉnh sửa</a></td>
						</tr>
						<?php } ?>
						<tr class="tbfooter">
							<td colspan="<?php if(isset($isuser) && $isuser == 0){echo '7';}else{echo '9';}?>">
								<span id="total"><?php $pager->drawResults($totalItems);?></span>
								<span id="pager"><?php $pager->drawPager($totalItems, $pageSize, $page);?></span>
							</td>
						</tr>
					</table>
					<div class="btArea">
						<input type="button" value="Thêm mới" onclick="window.location='./content-edit.php'"/>
						<input type="button" value="Xóa" id="btDelete" />
						<input type="hidden" name="action" id="crudaction">
					</div>
					<script type="text/javascript">
					$(function(){
						$("#btDelete").click(function(){
							if($('input[name="checkList[]"]:checked').length > 0){
								if(confirm('Bạn có chắc chắn khi xóa những phần tử đã chọn?')){
									$("#crudaction").val("delete");
									$("#formUserList").submit();
								}
							}else{
								alert('Bạn phải chọn ít nhất một phần tử');
							}
						});

						$("#btSearch").click(function(){
							$("#crudaction").val("search");
							$("#formUserList").submit();
						});
					});
					</script>	
				</form>
			</div>
		</center>
		<?php include '../include/footer.php';?>
	</body>
</html>
